package test;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Descroption
 * User:dell
 * Date:2025-09-09
 * Time:10:24
 */
public class MessageReplyPage extends Utils {
    public static String url = "http://81.70.248.108:58080/index.html";

    public MessageReplyPage() {
        super(url);
    }
    /**
     * 站内信的回复以及查看
     */
    public void messageReply() throws InterruptedException {
        WebDriverWait indexWait = new WebDriverWait(webDriver, Duration.ofSeconds(10));
        indexWait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[1]/header[1]/div/div/div[2]/div/a")));

        webDriver.findElement(By.xpath("/html/body/div[1]/header[1]/div/div/div[2]/div/a")).click();

        indexWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"offcanvasEndLabel\"]")));
        String boardTitle = webDriver.findElement(By.xpath("//*[@id=\"offcanvasEndLabel\"]")).getText();
        assert boardTitle.equals("所有站内信"):"获取站内信失败";


        //查看是否有消息
        indexWait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*[@id=\"index_div_message_list\"]/div/div/div[2]/a")));
        List<WebElement> webElements = webDriver.findElements(By.xpath("//*[@id=\"index_div_message_list\"]/div/div/div[2]/a"));
        if (webElements.size() <= 0){
            System.out.println("没有消息要回复");
            return;
        }
        //状态的改变

        // 1.已读状态
        webDriver.findElement(By.xpath("//*[@id=\"index_div_message_list\"]/div[1]/div/div[2]/a")).click();
        indexWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"index_message_reply_cancel\"]")));
        WebElement cancelEle = webDriver.findElement(By.xpath("//*[@id=\"index_message_reply_cancel\"]"));
        cancelEle.click();

        //2. 回复消息
        indexWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"index_div_message_list\"]/div[1]/div/div[2]/a")));
        webDriver.findElement(By.xpath("//*[@id=\"index_div_message_list\"]/div[1]/div/div[2]/a")).click();

        indexWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"btn_index_message_reply\"]")));
        webDriver.findElement(By.xpath("//*[@id=\"btn_index_message_reply\"]")).click();

        //等待消息输入区加载
        indexWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"index_message_reply_receive_content\"]")));
        WebElement contentEle = webDriver.findElement(By.xpath("//*[@id=\"index_message_reply_receive_content\"]"));
        contentEle.click();
        contentEle.sendKeys("回复成功");

        //发送信息
        webDriver.findElement(By.xpath("//*[@id=\"btn_index_send_message_reply\"]")).click();
    }
}
